package vn.student.controller;

import java.util.ArrayList;
import vn.student.AStudentDB;
import vn.student.R;
import vn.student.ViewContactDetail;
import vn.student.mylib.Constants;
import vn.student.mylib.Contact;
import vn.student.mylib.ContactList;
import vn.student.mylib.ListViewAdapter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;

public class ViewContactController implements AdapterView.OnItemClickListener{
	Activity act;
	public boolean isViewInstructor = true;
	public ViewContactController(Activity act){
		this.act = act;
		RelativeLayout viewInstructorContact = (RelativeLayout) act.findViewById(R.id.viewInstructorBtn);
		viewInstructorContact.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onClickViewInstructorContact();
			}
		});
		RelativeLayout viewStudentContact = (RelativeLayout) act.findViewById(R.id.viewStudentBtn);
		viewStudentContact.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onClickViewStudentContact();
			}
		});
		
		viewStudentContact = (RelativeLayout) act.findViewById(R.id.addNewContactBtn);
		viewStudentContact.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				addContact();
			}
		});
		
		ListView listView = (ListView) act.findViewById(R.id.list_contact);
		listView.setOnItemClickListener(this);
	}
	public void addContact(){
		Intent intent = new Intent(act,ViewContactDetail.class);
		Bundle bundle = new Bundle();
		bundle.putString("id","-1");
		bundle.putBoolean("isEdit", true);
		bundle.putBoolean("isInstructor", isViewInstructor);
		bundle.putBoolean("isCreateNew", true);
		intent.putExtras(bundle);
		act.startActivityForResult(intent,Constants.VIEW_ADD_NEW_INSTRUCTOR_EDITOR);
	}
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		
		Intent intent = new Intent(act,ViewContactDetail.class);
		Bundle bundle = new Bundle();
		ListViewAdapter adapter = (ListViewAdapter) arg0.getAdapter();
		ArrayList<Contact> list = adapter.getContactInfoList();
		bundle.putString("id",list.get(arg2).getId());
		bundle.putBoolean("isEdit", false);
		bundle.putBoolean("isInstructor", isViewInstructor);
		bundle.putBoolean("isCreateNew", false);
		intent.putExtras(bundle);
		act.startActivityForResult(intent,3);
	}
	public void onClickViewInstructorContact(){
		isViewInstructor = true;
		ListView listView = (ListView) act.findViewById(R.id.list_contact);
		getContactListView(listView,"gv");
	}
	public void onClickViewStudentContact(){
		isViewInstructor = false;
		ListView listView = (ListView) act.findViewById(R.id.list_contact);
		getContactListView(listView,"sv");
	}
	public void getContactListView(ListView listView,String n){
		AStudentDB myDB = new AStudentDB(act);
		ContactList list = null;
        if(myDB!=null){
        	list = myDB.getContactList(n);
        }
        if(list!=null){
        	listView.setAdapter(new ListViewAdapter(act,list.getContactInfoList()));
        	listView.setOnCreateContextMenuListener(new OnCreateContextMenuListener()  {
				@Override
				public void onCreateContextMenu(ContextMenu menu, View v,
						ContextMenuInfo menuInfo) {
					menu.add(0,Constants.CONTACT_CONTEXTMENU_ADDNEW,0, "Add new");
					menu.add(0,Constants.CONTACT_CONTEXTMENU_EDITITEM,1, "Edit");
					menu.add(0,Constants.CONTACT_CONTEXTMENU_DELETEITEM,2, "Delete");
				}
           }); 
        }
	}
}
